package com.wxtt.android.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;

import com.fast.dev.frame.Adapter.listview.BaseListAdapter;
import com.fast.dev.frame.http.callback.ResultHttpCallBack;
import com.fast.dev.frame.utils.AndroidInfoUtils;
import com.wxtt.android.R;
import com.wxtt.android.activity.AriticleDetailActivity;
import com.wxtt.android.adapter.ArticleAdapter;
import com.wxtt.android.bean.WxArticleBean;
import com.wxtt.android.bean.WxQueryResultBean;
import com.wxtt.android.config.ApiConfig;

import java.util.ArrayList;

/**
 * 说明：微信精选内容
 * <p/>
 * 作者：fanly
 * <p/>
 * 时间：2016/1/8 17:11
 * <p/>
 * 版本：verson 1.0
 */
public class WxFragment extends BaseListFragment<WxArticleBean> {

    //当前请求的页码
    private int currentPage = 0;
    //每页请求的条数
    private int pageNumber = 20;
    //保存的数据
    private ArrayList<WxArticleBean> mAriticleDatas = new ArrayList<>();
    private ArticleAdapter articleAdapter;

    @Override
    public BaseListAdapter<WxArticleBean> setAdapter() {
        articleAdapter = new ArticleAdapter(lv,mAriticleDatas, R.layout.item_wx_article);
        return articleAdapter;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(getActivity(), AriticleDetailActivity.class);
        Bundle bundle = new Bundle();
        bundle.putParcelable("detailBundle", mAriticleDatas.get(position));
        intent.putExtra("detailBundle", bundle);
        getActivity().startActivity(intent);
    }


    /**
     * 说明：获取网络数据
     * @param curPage 当前页码
     * @param pNumber 每页条数
     */
    private void readNetData(int curPage,int pNumber,ResultHttpCallBack<WxQueryResultBean> callback){
        ApiConfig.queryWXJX(curPage, pNumber, callback);
    }


    @Override
    public void onFirstUserVisible() {
        super.onFirstUserVisible();
        empty_bridge.setMyView(swipe_refresh);
        initFirst();
    }

    private void initFirst(){
        empty_bridge.setLoadingState();
        if (AndroidInfoUtils.isNetConnected(mActivity)){
            swipe_refresh.autoRefresh();
        }else {
            empty_bridge.setNetWorkError(getString(R.string.not_network),erroListener);
        }
    }

    private View.OnClickListener erroListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            initFirst();
        }
    };

    @Override
    public void onRefresh() {
        if (currentPage < 0) currentPage = 0;
        currentPage++;
        readNetData(currentPage, pageNumber, new ResultHttpCallBack<WxQueryResultBean>() {
            @Override
            public void onSuccess(WxQueryResultBean result) {
                mAriticleDatas.addAll(0, result.getResult().getList());
                articleAdapter.refresh(mAriticleDatas);
            }

            @Override
            public void onFailure(int errorCode, String msg) {
                currentPage--;
                if (mAriticleDatas.isEmpty()){
                    empty_bridge.setFailState(getString(R.string.loading_fail),erroListener);
                }else {
                    mActivity.shortMessage(lv, msg);
                }
            }

            @Override
            public void onFinish() {
                swipe_refresh.setRefreshLoadedState();
                if (!mAriticleDatas.isEmpty()){
                    empty_bridge.setSuccessState();
                }
            }
        });
    }

}
